package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.CollectionKit;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.Material;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.roles.TeacherRoles;
import com.wxxymaker.edu.service.MaterialService;
import com.wxxymaker.edu.utils.FileUtils;
import io.github.biezhi.anima.page.Page;

import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/10/6 20:44
 * @DESCRIBE 资料接口 起码要是个学生才能浏览到资料
 */
@Path(value = "/course",restful = true)
public class MaterialHandlerApi {

    @Inject
    private MaterialService materialService;

    /**
     * 增加下载量
     * @param mid 资源id
     */
    @StudentRoles
    @PostRoute("/material/api/downs/:mid")
    public RestResponse addDowns(@PathParam Integer mid) {
        if (materialService.down(mid)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 获取没有使用的视频文件
     * @param kid 课程id
     */
    @TeacherRoles
    @PostRoute("/material/api/:kid/nouse")
    public RestResponse noUseVideo(@PathParam Integer kid) {
        List<Material> noUseVideo = materialService.getNoUseVideo(kid);
        if (CollectionKit.isNotEmpty(noUseVideo)) {
            return RestResponse.ok(noUseVideo);
        }
        return RestResponse.fail("没有空闲的资源！");
    }

    @StudentRoles
    @PostRoute("/material/api/:kid/page")
    public RestResponse page(@PathParam Integer kid,
                             @Param String type,
                             @Param(defaultValue = "true") boolean join,  // 是否加入课程信息
                             @Param(defaultValue = "1") Integer page,
                             @Param(defaultValue = "10") Integer size) {
        Page<Material> page1 = materialService.page(kid, type, page, size, join);
        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail();
    }

    @StudentRoles
    @PostRoute("/material/api/:kid/one/:tid")
    public RestResponse one(@PathParam Integer tid) {
        Material material = materialService.byId(tid);
        if (material != null) {
            return RestResponse.ok(material);
        }
        return RestResponse.fail();
    }

    @TeacherRoles
    @PostRoute("/set/material/api/:kid/delete/:mid")
    public RestResponse delete(@PathParam Integer mid,
                               @Param boolean other) {
        if (other || !materialService.isUse(mid)) {
            Material material = materialService.delete(mid, other);
            if (material != null) {
                return RestResponse.ok(material);
            }
        } else {
            return RestResponse.fail("资料正作为课件使用,禁止删除!");
        }
        return RestResponse.fail();
    }

    @TeacherRoles
    @PostRoute("/set/material/:kid/save")
    public RestResponse save(Material material) {
        if (material.getId() == null || material.getId()<0) {
            String path = material.getPath();
            material.setPath(path.replace("/temp", ""));
            Material add = materialService.add(material);
            if (add != null) {
                FileUtils.saveTemp(path);
                return RestResponse.ok(add);
            }
        }
        return RestResponse.fail();
    }
}
